<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 14:34
 */

namespace app\vehicle\logic;
use think\Db;
use app\vehicle\model\User;
use app\vehicle\model\Site;
use app\vehicle\model\Exchange;
use app\vehicle\model\Volunteer;
use app\vehicle\model\ExchangeLog;
use app\vehicle\model\UserIntegral;
use app\vehicle\model\SiteTeam;
use app\vehicle\model\UserIntegralLog;
class integralLogic
{
    public function checkCode($data,$uid){
        $info = ExchangeLog::where('code','=',$data['code'])->find();
        $site_id = Site::where('find_in_set('.$uid.',role_id)')->value('id');
        if(!$site_id){
            return '您暂无核销权限';
        }else if($site_id!=$info['site_id']){
            return '您暂无该站点核销权限';
        }
        if(!$info['id']){
            return '核销码错误'.json_encode($data);
        }else if($info['status']==1){
            return '已核销';
        }
        ExchangeLog::where('id','eq',$info['id'])->update(['status'=>1,'update_time'=>time()]);
        return 1;
    }
    public function getUserIntegralLog($data,$user_id){
        $site_id = Site::where('find_in_set('.$user_id.',pass_id)')->value('id');
        if(!$site_id){
            return ['msg'=>'暂无权限'];
        }
        $allId = Volunteer::where('id','=',$data['vid'])->value('user_id');
        if(!$allId){
            return ['msg'=>'该存折不存在'];
        }
        //$allId = implode(',',Volunteer::where('site_id','=',$site_id)->where('is_after','=',1)->column('user_id'));
        $where[] = ['user_id','in',$allId];
        $list = ExchangeLog::with(['exchange','userName'])->where($where)->page($data['page'],10)->order('id desc')->select();
        return $list;
    }
    public function getIntegralGoods($data,$user_id=0,$site_id=0){
        $time = time();
        $where[] = ['status','eq',1];
        $where[] = ['use_time','<',$time];
        $where[] = ['end_time','>',$time];
        $where[] = ['number','>',0];
        $where[] = ['site_id','=',$site_id];
        $list = Exchange::where($where)->select();
        foreach($list as $k=>$v){
            $list[$k]['is_exchange'] = ExchangeLog::where('user_id','eq',$user_id)->where('exchange_id','eq',$v['id'])->count();
        }
        return $list;
    }
    public function getIntegralInfo($data,$user_id){
        $info = Exchange::get($data['id']);
        $info['use_integral'] = User::where('id','eq',$user_id)->value('integral');
        $info['is_exchange'] = ExchangeLog::where('user_id','eq',$user_id)->where('exchange_id','eq',$data['id'])->count();
        $info['code'] = ExchangeLog::where('user_id','eq',$user_id)->where('exchange_id','eq',$data['id'])->value('code');
        return $info;
    }
    public function getExchangeList($user_id){
        return ExchangeLog::with('exchange')->where('user_id','eq',$user_id)->order('id desc')->select();
    }
    public function setExchange($data,$user_id,$openid){
        $check = ExchangeLog::where('user_id','eq',$user_id)->where('exchange_id','eq',$data['id'])->count();
//        if($check){
//            return '已兑换';
//        }
        $info = Exchange::get($data['id']);
        $user = User::get($user_id);
        if($user['integral']<$info['integral']){
            return '积分不足';
        }else if(!$info['number']){
            return '商品不足';
        }
        $site_id = Volunteer::where('uid','=',$user_id)->value('site_id');
        if($info['site_id']!=$site_id){
            return '您未绑定该站点，无法兑换。';
        }
        $time = time();
        Db::startTrans();
        try {
            $insert['user_id'] = $user_id;
            $insert['exchange_id'] = $data['id'];
            $insert['create_time'] = $time;
            $insert['mobile'] = $data['mobile'];
            //$strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
            $strs = '1234567890';
            $insert['code'] = substr(str_shuffle($strs),mt_rand(0,strlen($strs)-10),10);

            ExchangeLog::insert($insert);

            Exchange::where('id','eq',$data['id'])->setDec('number');

            $log['user_id'] = $user_id;
            $log['type_id'] = 1;
            $log['exchange_id'] = $data['id'];
            $log['integral'] = $info['integral'];
            $log['create_time'] = $time;
            UserIntegralLog::insert($log);

            User::where('id','eq',$user_id)->setDec('integral',$info['integral']);
            TplLogic::exchangeTpl($openid,$info['title'],$info['integral']);
            Db::commit();
            return 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
    }
    public function setUserExchange($data,$uid,$openid){
        $site_id = Site::where('find_in_set('.$uid.',pass_id)')->value('id');
        if(!$site_id){
            return '暂无权限';
        }
        $user_id =  Volunteer::where('id','=',$data['vid'])->value('user_id');
        if(!$user_id){
            return '该存折不存在';
        }
        $check = ExchangeLog::where('user_id','eq',$user_id)->where('exchange_id','eq',$data['id'])->count();
        $info = Exchange::get($data['id']);
        $user = User::get($user_id);
        if($user['integral']<$info['integral']){
            return '积分不足';
        }else if(!$info['number']){
            return '商品不足';
        }
        $time = time();
        Db::startTrans();
        try {
            $insert['user_id'] = $user_id;
            $insert['exchange_id'] = $data['id'];
            $insert['create_time'] = $time;
            $insert['mobile'] = $data['mobile'];
            $strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
            $insert['code'] = substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),10);
            $insert['status'] = 1;
            $insert['update_time'] = time();
            ExchangeLog::insert($insert);

            Exchange::where('id','eq',$data['id'])->setDec('number');

            $log['user_id'] = $user_id;
            $log['type_id'] = 1;
            $log['exchange_id'] = $data['id'];
            $log['integral'] = $info['integral'];
            $log['create_time'] = $time;
            UserIntegralLog::insert($log);

            User::where('id','eq',$user_id)->setDec('integral',$info['integral']);
            TplLogic::exchangeTpl($openid,$info['title'],$info['integral']);
            Db::commit();
            return 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
    }
    public static function checkTime($user_id,$t){
        return UserIntegralLog::where('user_id','eq',$user_id)->where('type_id','eq',0)->where('create_time','>',$t)->count();
    }
    public function setIntegral($user_id,$openid){
        $time = time();
        $t = strtotime(date('Y-m-d'));
        $check = UserIntegralLog::where('user_id','eq',$user_id)->where('type_id','eq',0)->where('create_time','>',$t)->count();
        if($check){
            return '已签到';
        }
        $id = 1;
//        $check1 = self::checkTime($user_id,$t-86400);
//        if($check1){
//            $id = 2;
//            $check2 = self::checkTime($user_id,$t-86400*2);
//            if($check2){
//                $id = 3;
//                $check3 = self::checkTime($user_id,$t-86400*3);
//                if($check3){
//                    $id = 4;
//                    $check4 = self::checkTime($user_id,$t-86400*4);
//                    if($check4){
//                        $id = 5;
//                        $check5 = self::checkTime($user_id,$t-86400*5);
//                        if($check5){
//                            $id = 6;
//                            $check6 = self::checkTime($user_id,$t-86400*6);
//                            if($check6){
//                                $id = 7;
//                            }
//                        }
//                    }
//                }
//            }
//        }
        $integral = UserIntegral::where('id','eq',$id)->value('integral');
        $insert['user_id'] = $user_id;
        $insert['integral'] = $integral;
        $insert['create_time'] = $time;
        $insert['remark'] = '用户签到';
        UserIntegralLog::insert($insert);
        TplLogic::signTpl($openid,$integral);
        User::where('id','eq',$user_id)->setInc('integral',$integral);
        return 1;
    }
    public function getIntegralList($user_id){
        $list = UserIntegralLog::where('user_id','eq',$user_id)->order('id desc')->select();
        return $list;
    }

    public function getUserIntegralList($data,$uid){
        $site_id = Site::where('find_in_set('.$uid.',pass_id)')->value('id');
        if(!$site_id){
            return '暂无权限';
        }
        $user_id = Volunteer::where('id','=',$data['vid'])->value('user_id');
        //$user_id = implode(',',Volunteer::where('site_id','=',$site_id)->where('is_after','=',1)->column('user_id'));
        $list = UserIntegralLog::with('user')->where('user_id','in',$user_id)->page($data['page'],20)->order('id desc')->select();
        return $list;
    }
}